//
//  EFModifyPasswordViewController.m
//  J智现场
//
//  Created by song on 2017/12/13.
//  Copyright © 2017年 王长飞. All rights reserved.
//

#import "EFSendMsgModel.h"
#import "ChangePwdModel.h"
#import "ChangePwdSmsModel.h"
#import "ModifyPasswordController.h"
#import "LoginViewController.h"
#import "EFNavigationController.h"
#import <objc/runtime.h>
//#import "AFHTTPSessionManager.h"
#import <CommonCrypto/CommonDigest.h>


@interface TypeBtn : UIButton

@property(nonatomic,strong) UIButton * leftBtn;
@property(nonatomic,strong) UILabel * titLabel;

@end

@implementation TypeBtn

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        _leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width/2-60,(self.frame.size.height-25)/2, 25, 25)];
        [_leftBtn setImage:[UIImage imageNamed:@"default_unSelect"] forState:UIControlStateNormal];
        [_leftBtn setImage:[UIImage imageNamed:@"default_select"] forState:UIControlStateSelected];
        [self addSubview:_leftBtn];
        
        _titLabel = [[UILabel alloc] initWithFrame:CGRectMake(_leftBtn.right + 10, 0, self.frame.size.width - _leftBtn.right-10,self.frame.size.height)];
        _titLabel.textColor = kTitleColor_black;
        _titLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_titLabel];
        
    }
    
    return self;
}

@end


@interface ModifyPasswordController ()<UIScrollViewDelegate,BaseModelProtocol>

@property (nonatomic,strong)EFSendMsgModel *msgModel;
@property (nonatomic,strong)ChangePwdModel *changePwdModel;
@property (nonatomic,strong)ChangePwdSmsModel *changePwdSmsModel;

@property (nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic, strong)UIView *topSelectView;
@property(nonatomic, assign)BOOL isCode;

@property(nonatomic, strong)UIView *orignalPasswordView;
@property(nonatomic, strong)UITextField *orignalPasswordField;

@property(nonatomic, strong)UIView *codeView;
@property(nonatomic, strong)UITextField *codeTextField;

@property(nonatomic, strong)UIView *passwordView;
@property(nonatomic, strong)UITextField *passwordField;

@property(nonatomic, strong)UIView *confirmPasswordView;
@property(nonatomic, strong)UITextField *confirmPasswordField;

@property(nonatomic, strong)UIButton *sendMessageBtn;

@property (nonatomic,strong) NSTimer  *getCodeTimer ;
@property (nonatomic,assign) int timeCount ;

@end

@implementation ModifyPasswordController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (self.getCodeTimer){
        [self.getCodeTimer invalidate];
        self.getCodeTimer = nil ;
    }
}

- (void)initView
{
    [super initView];
    [self.navBarView addTitle:@"修改密码"];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.topSelectView];
    [self.scrollView addSubview:self.orignalPasswordView];
    [self.scrollView addSubview:self.codeView];
    [self.codeView setHidden:YES];
    self.isCode = NO;
    [self.scrollView addSubview:self.passwordView];
    [self.scrollView addSubview:self.confirmPasswordView];
    
    UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-160)/2, self.confirmPasswordView.bottom + 60, 160, 40)];
    resetBtn.layer.cornerRadius = 20;
    resetBtn.backgroundColor = UIColorFromRGB1(0xE5B580);
    [resetBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [resetBtn setTitleColor:kTitleColor_black forState:UIControlStateNormal];
    [resetBtn.titleLabel setFont:kFontMidden];
    [resetBtn addTarget:self action:@selector(resetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:resetBtn];
    
    self.timeCount = 60 ;
}

- (void)sendMessageClick:(UIButton *)btn{
    
    EFAppContext *appc = [EFAppContext sharedContext];
    [self.msgModel getSendMessageWithMobile:appc.mobile];
}

- (void)handleModelError:(id)sender error:(NSError *)error{
    
    [self hideHUD];
}

- (void)getDataFinish:(BaseModel *)baseModel netResponse:(BaseResponse *)response{
    
    [self hideHUD];
    
    if ([baseModel isKindOfClass:[EFSendMsgModel class]]) {
        
        [EFShowView showStatus:ShowSuccess text:@"验证码已发送"  InView:self.view];
        
        self.getCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFire) userInfo:nil repeats:YES];
        [self.getCodeTimer fire];
        
    }
    else if ([baseModel isKindOfClass:[ChangePwdModel class]]){

//        [EFShowView showText:@"修改成功,请重新登录"];
//        [self performSelector:@selector(gotoLoginVC) withObject:nil afterDelay:1];
        [EFShowView showText:@"修改成功!"];
//        [self backWithDelay:0.5];
        [self performSelector:@selector(logOut) withObject:nil afterDelay:1];
    }
    else if ([baseModel isKindOfClass:[ChangePwdSmsModel class]]){
        [EFShowView showText:@"修改成功!"];
//        [self backWithDelay:0.5];
        [self performSelector:@selector(logOut) withObject:nil afterDelay:1];
    }
}

- (void)logOut{
    LoginViewController *login = [[LoginViewController alloc]init];
//    EFNavigationController *nav = [[EFNavigationController alloc]initWithRootViewController:login];
//    [self.navigationController presentViewController:nav animated:YES completion:nil];
    [self.navigationController pushViewController:login animated:YES];
}

- (void)gotoLoginVC{

    //清除用户信息
    [[EFAppContext sharedContext] clear];
    LoginViewController *login = [[LoginViewController alloc]init];
    EFNavigationController *nav = [[EFNavigationController alloc]initWithRootViewController:login];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

- (void)timerFire
{
    _sendMessageBtn.userInteractionEnabled = NO ;
    [_sendMessageBtn setTitle:[NSString stringWithFormat:@"重发( %d s)",self.timeCount--] forState:UIControlStateNormal];
    
    if (_timeCount<0) {
        _timeCount = 60 ;
        _sendMessageBtn.userInteractionEnabled = YES ;
        [_sendMessageBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.getCodeTimer invalidate]; self.getCodeTimer = nil;
    }
}

-(void)resetBtnClick{
    
    if(self.isCode){
        if (ISEMPTY(self.codeTextField.text)) {
            [EFShowView showStatus:ShowInfo text:@"请输入验证码"  InView:self.view];
            return ;
        }
    }else{
        if (ISEMPTY(self.orignalPasswordField.text)) {
            [EFShowView showStatus:ShowInfo text:@"请输入原密码"  InView:self.view];
            return ;
        }
    }
    
    if (ISEMPTY(self.passwordField.text)) {
        [EFShowView showStatus:ShowInfo text:@"请输入新密码"  InView:self.view];
        return ;
    }
    if (ISEMPTY(self.confirmPasswordField.text)) {
        [EFShowView showStatus:ShowInfo text:@"请确认密码"  InView:self.view];
        return ;
    }

    if (![self.passwordField.text isEqualToString:self.confirmPasswordField.text]) {
        [EFShowView showStatus:ShowError text:@"两次密码不一致"  InView:self.view];
        return;
    }
    
    [self showHUD];
    
    if(self.isCode){
        [self.changePwdSmsModel changePwdWith:[self SHA256WithKey:self.passwordField.text] smsCode:self.codeTextField.text];
    }else{
        [self.changePwdModel changePwdWith:[self SHA256WithKey:self.passwordField.text] oldPassword:[self SHA256WithKey:self.orignalPasswordField.text]];
    }
}


- (NSString *)SHA256WithKey:(NSString *)srcString{

    const char *string = [srcString UTF8String];
    unsigned char result[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(string, (CC_LONG)strlen(string), result);

    NSMutableString *hashed = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
      [hashed appendFormat:@"%02x", result[i]];
    }
    NSLog(@"hashed===============");
    NSLog(hashed);
    return hashed;

}

- (void)orignalPasswordBtnClick:(TypeBtn *)btn{
    
    btn.selected = YES;
    TypeBtn * typeBtn = [self.scrollView viewWithTag:200];
    typeBtn.leftBtn.selected = NO;
    [self.orignalPasswordView setHidden:NO];
    [self.codeView setHidden:YES];
    self.isCode = NO;

}

- (void)codeBtnClick:(TypeBtn *)btn{

    btn.selected = YES;
    TypeBtn * typeBtn = [self.scrollView viewWithTag:100];
    typeBtn.leftBtn.selected = NO;
    [self.orignalPasswordView setHidden:YES];
    [self.codeView setHidden:NO];
    self.isCode = YES;

}

- (UIView *)topSelectView{
    if(nil == _topSelectView){
        _topSelectView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, self.scrollView.width, 50)];
        
        TypeBtn *orignalPasswordBtn = [[TypeBtn alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.width/2, 45)];
        orignalPasswordBtn.tag = 100;
        orignalPasswordBtn.titLabel.text = @"原密码";
        [orignalPasswordBtn.leftBtn addTarget:self action:@selector(orignalPasswordBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        orignalPasswordBtn.leftBtn.selected = YES;
        [_topSelectView addSubview:orignalPasswordBtn];
        
        TypeBtn *codeBtn = [[TypeBtn alloc] initWithFrame:CGRectMake(orignalPasswordBtn.right, 0, orignalPasswordBtn.width, 45)];
        codeBtn.tag = 200;
        codeBtn.titLabel.text = @"短信验证";
        [codeBtn.leftBtn addTarget:self action:@selector(codeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_topSelectView addSubview:codeBtn];

    }
    return _topSelectView;
}

- (UIView *)orignalPasswordView{
    if(nil == _orignalPasswordView){
        _orignalPasswordView = [[UIView alloc]initWithFrame:CGRectMake(15, 100, _scrollView.width-30, 36)];
        _orignalPasswordView.backgroundColor = UIColorFromRGB1(0xfff7f6f6);
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 60, _orignalPasswordView.height)];
        lable.text = @"原密码";
        lable.textColor = kTitleColor_midden;
        lable.font = KFontSmall;
        [_orignalPasswordView addSubview:lable];
        
        _orignalPasswordField = [[UITextField alloc] initWithFrame:CGRectMake(lable.right+30, 0, _orignalPasswordView.width-lable.right-50, _orignalPasswordView.height)];
        _orignalPasswordField.placeholder = @"请输入原密码";
        _orignalPasswordField.textColor = kTitleColor_light;
        _orignalPasswordField.font = KFontSmall;
        [_orignalPasswordView addSubview:_orignalPasswordField];
//        _passwordField.secureTextEntry = YES;
    }
    return  _orignalPasswordView;
}

- (UIView *)codeView{
    if(nil == _codeView){
        _codeView = [[UIView alloc]initWithFrame:CGRectMake(15, _orignalPasswordView.top, _scrollView.width-30, 36)];
        _codeView.backgroundColor = UIColorFromRGB1(0xfff7f6f6);
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 60, _codeView.height)];
        lable.text = @"验证码";
        lable.textColor = kTitleColor_midden;
        lable.font = KFontSmall;
        [_codeView addSubview:lable];
        
        _codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(lable.right+30, 0, _codeView.width-lable.right-50-30, _codeView.height)];
        _codeTextField.placeholder = @"请输入验证码";
        _codeTextField.textColor = kTitleColor_light;
        _codeTextField.font = KFontSmall;
        [_codeView addSubview:_codeTextField];
//        _passwordField.secureTextEntry = YES;
        
        _sendMessageBtn = [[UIButton alloc] initWithFrame:CGRectMake(_codeView.width-76, 7, 70, 22)];
        [_sendMessageBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _sendMessageBtn.titleLabel.font = [UIFont systemFontOfSize:11];
        [_sendMessageBtn setTitleColor:kTitleColor_midden forState:UIControlStateNormal];
        [_sendMessageBtn setCornerDefaultHalf];
        _sendMessageBtn.backgroundColor = UIColorFromRGB1(0xE5B580);
        [_sendMessageBtn addTarget:self action:@selector(sendMessageClick:) forControlEvents:UIControlEventTouchUpInside];
        [_codeView addSubview:_sendMessageBtn];
    }
    return  _codeView;
}

- (UIView *)passwordView{
    if(nil == _passwordView){
        _passwordView = [[UIView alloc]initWithFrame:CGRectMake(15, _orignalPasswordView.bottom+15, _scrollView.width-30, 36)];
        _passwordView.backgroundColor = UIColorFromRGB1(0xfff7f6f6);
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 60, _passwordView.height)];
        lable.text = @"新密码";
        lable.textColor = kTitleColor_midden;
        lable.font = KFontSmall;
        [_passwordView addSubview:lable];
        
        _passwordField = [[UITextField alloc] initWithFrame:CGRectMake(lable.right+30, 0, _passwordView.width-lable.right-50, _passwordView.height)];
        _passwordField.placeholder = @"请输入新密码";
        _passwordField.textColor = kTitleColor_light;
        _passwordField.font = KFontSmall;
        [_passwordView addSubview:_passwordField];
//        _passwordField.secureTextEntry = YES;
    }
    return  _passwordView;
}

- (UIView *)confirmPasswordView{
    if(nil == _confirmPasswordView){
        _confirmPasswordView = [[UIView alloc]initWithFrame:CGRectMake(15, _passwordView.bottom+15, _scrollView.width-30, 36)];
        _confirmPasswordView.backgroundColor = UIColorFromRGB1(0xfff7f6f6);
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 60, _confirmPasswordView.height)];
        lable.text = @"确认密码";
        lable.textColor = kTitleColor_midden;
        lable.font = KFontSmall;
        [_confirmPasswordView addSubview:lable];
        
        _confirmPasswordField = [[UITextField alloc] initWithFrame:CGRectMake(lable.right+30, 0, _confirmPasswordView.width-lable.right-50, _confirmPasswordView.height)];
        _confirmPasswordField.placeholder = @"请确认密码";
        _confirmPasswordField.textColor = kTitleColor_light;
        _confirmPasswordField.font = KFontSmall;
        [_confirmPasswordView addSubview:_confirmPasswordField];
    }
    return  _confirmPasswordView;
}

- (UIScrollView *)scrollView{
    
    if (nil == _scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, KNavFullHeight+10, SCREEN_WIDTH-20, SCREEN_HEIGHT_EXCEPTNAV-30)];
        _scrollView.delegate = self ;
        _scrollView.backgroundColor = [UIColor whiteColor];
        [_scrollView setCornerRedius:5];
        _scrollView.alwaysBounceVertical = YES ;
    }
    return _scrollView;
}

- (EFSendMsgModel *)msgModel{
    
    if (nil == _msgModel) {
        _msgModel = [[EFSendMsgModel alloc]initWithDelegate:self cache:NO];
    }
    return _msgModel;
}

- (ChangePwdModel *)changePwdModel{

    if (nil == _changePwdModel) {
        _changePwdModel = [[ChangePwdModel alloc]initWithDelegate:self cache:NO];
    }
    return _changePwdModel;
}

- (ChangePwdSmsModel *)changePwdSmsModel{

    if (nil == _changePwdSmsModel) {
        _changePwdSmsModel = [[ChangePwdSmsModel alloc]initWithDelegate:self cache:NO];
    }
    return _changePwdSmsModel;
}
@end
